
package com.rongshu.sendmessage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.activity.seeImages.PictureDisplayActivityTwo;
import com.rongshu.sendmessage.bean.HealthyRegimenList;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.itemDecoration.MyItemDecoration3;

import java.util.ArrayList;
import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.MyViewHoler> implements View.OnClickListener {
    private int ResourceID;
    private Context mContext;
    private List<HealthyRegimenList> mList;
    private OnRecycleViewItemClickListener mOnItemClickListener;
    private ImageLoader imageLoader;
    private ImageDataAdapter baseDataAdapterMain;

    public List<HealthyRegimenList> getmList() {
        return mList;
    }

    public void setmList(List<HealthyRegimenList> mList) {
        this.mList = mList;
    }

    public MessageListAdapter(Context context, int resourceID, List<HealthyRegimenList> list) {
        mContext = context;
        mList = list;
        ResourceID = resourceID;
    }
    @Override
    public void onBindViewHolder(MyViewHoler holder, int position) {
        //相当于listview的adapter中的getview方法
        //负责将数据绑定到视图上
        if (imageLoader == null)
            imageLoader = ImageLoader.getInstance();
        HealthyRegimenList messageList = mList.get(position);
//        holder.tv_title.setText(StringUtils.isEmpty(messageList.getContentType())?"暂无消息":messageList.getContentType());
//        if(StringUtils.isEmpty(messageList.getImgUrlList())){
//            ImageLoader.getInstance().displayImage("https://ryb.rongskj.com/ryb/uploads/20210809/1a0ac1ab-c7bf-4614-a06b-993013a4614b.jpg",holder.iv_head_image);
//        }else
//            ImageLoader.getInstance().displayImage(messageList.getReleaseUserHeadPortrait(),holder.iv_head_image);
        holder.tv_content.setText(StringUtils.isEmpty(messageList.getSmsContent())?"暂无内容":messageList.getSmsContent());
        holder.tv_time.setText(messageList.getCreateTime());
        holder.rl_images.setVisibility(View.GONE);
        holder.ll_bianji.setTag(position);
        holder.ll_bianji.setOnClickListener(this);
        holder.card_item.setTag(position);//将位置保存在tag中
        holder.card_item.setOnClickListener(this);
    }
    //初始化图片的recycleView
    private void initRecycleView(RecyclerView rl_images,List<String>images) {
        GridLayoutManager layoutmanager = new GridLayoutManager(mContext, 3);
        layoutmanager.setOrientation(GridLayoutManager.VERTICAL);
        //设置RecyclerView 布局管理器
        rl_images.setLayoutManager(layoutmanager);
        //把自定义的ItemDecoration设置给RecyclerView
        rl_images.addItemDecoration(new MyItemDecoration3());
        //设置设置RecyclerView的适配器Adapter
        baseDataAdapterMain = new ImageDataAdapter(images);
        rl_images.setAdapter(baseDataAdapterMain);
        baseDataAdapterMain.setOnItemClickListener(new ImageDataAdapter.OnItemClickListener() {
            @Override
            public void onLongClick(int position) {

            }
            @Override
            public void onClick(int p) {
                //查看图片，
                ArrayList<String> list = (ArrayList<String>) images;
                String path= list.get(p);
                Intent intent = new Intent(mContext, PictureDisplayActivityTwo.class);
                intent.putExtra("img_url",path+"");
                intent.putExtra("index",p);
                intent.putExtra("isCanDelete",false);
                try {
                    intent.putStringArrayListExtra("uris", list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public MyViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        //负责创建视图
        View view = LayoutInflater.from(mContext).inflate(ResourceID, null);
        return new MyViewHoler(view);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.OnItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecycleViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnRecycleViewItemClickListener {
        void OnItemClick(View view, int position);
    }

    class MyViewHoler extends RecyclerView.ViewHolder {

        private final ImageView iv_head_image;
        private final TextView tv_title,tv_content,tv_time,tv_juli;
        private final RecyclerView rl_images;
        private final LinearLayout ll_bianji;
        private final CardView card_item;

        public MyViewHoler(View itemView) {
            super(itemView);
            iv_head_image=itemView.findViewById(R.id.iv_head_image);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_content=itemView.findViewById(R.id.tv_content);
            rl_images=itemView.findViewById(R.id.rl_images);
            tv_time=itemView.findViewById(R.id.tv_time);
            tv_juli=itemView.findViewById(R.id.tv_juli);
            ll_bianji=itemView.findViewById(R.id.ll_bianji);
            card_item=itemView.findViewById(R.id.card_item);
        }
    }
}
